package com.awifi.demo.reflect;

import com.awifi.video.cloud.common.constant.ErrorContents;
import com.awifi.video.cloud.common.exception.VideoCloudException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;

/**
　　* @Description: 反射判断对象属性是否为空工具类
　　* @Author Marin
　　* @Date 2020/9/23 17:22
　　*/
public class ParamCheck {

    private static final Logger LOGGER = LoggerFactory.getLogger(ParamCheck.class);

    /**
    　　* @description: 检查所有属性是否为空
    　　* @param {Object，指定对象}
    　　* @return {}
    　　* @author Marin
    　　* @date 2020/9/23 17:28
    　　*/
    public static void checkParamsIsNull(Object obj) {
        try {
            for(Field f : obj.getClass().getDeclaredFields()){
                f.setAccessible(true);
                if(f.get(obj) == null || f.get(obj).equals("")){
                    LOGGER.error(f.getName()+"不能为空");
                    throw new VideoCloudException(ErrorContents.ERROR_NUM_006,f.getName());
                }
            }
        } catch (Exception e) {
            LOGGER.error("参数校验异常：{}",e.getMessage());
        }
    }

    /**
    　　* @description: 校验指定参数不能为空
    　　* @param {Object：指定对象，params：指定属性集}
    　　* @return {}
    　　* @author Marin
    　　* @date 2020/9/23 17:29
    　　*/
    public static void checkParamsIgnoreSome(Object obj,String ...params) {

        Field[] declaredFields = obj.getClass().getDeclaredFields();

        for (int i = 0; i < declaredFields.length; i++) {
            for (String param : params) {
                declaredFields[i].setAccessible(true);
                if (declaredFields[i].getName().equals(param)) {
                    break;
                }
                    try {
                        if (declaredFields[i].get(obj) == null || declaredFields[i].get(obj).equals("")) {
                            LOGGER.error(declaredFields[i].getName() + "不能为空");
                            throw new VideoCloudException(ErrorContents.ERROR_NUM_006, declaredFields[i].getName());
                        }
                    } catch (IllegalAccessException e) {
                        throw new VideoCloudException("解析属性异常：{}", e.getMessage());
                    }
            }
        }
    }
       /* try {
            for(Field f : obj.getClass().getDeclaredFields()) {
                f.setAccessible(true);
                LOGGER.info(f.getName());
                if(f.get(obj) == null || f.get(obj).equals("")){
                    LOGGER.error(f.getName()+"不能为空");
                    throw new VideoCloudException(ErrorContents.ERROR_NUM_006,f.getName());
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }*/



    public static void main(String[] args){
        BaseResult baseResult = new BaseResult();
        //baseResult.setData("hello");
        baseResult.setErrCode("2020");
        baseResult.setErrorMsg("error");
        //baseResult.setSubMsg("yy");
        //baseResult.setSubCode("11");
        //ParamCheck.checkParamsIsNull(baseResult);
        System.out.println("-------------");
        ParamCheck.checkParamsIgnoreSome(baseResult,"subMsg","subCode");
    }

}
